package com.zjw.zy.componse.demo.base;

import android.annotation.SuppressLint;
import android.util.Pair;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/12/1 10:43
 */
public abstract class BaseExpandRecyclerAdapter<Parent, Child> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected static final int PARENT_TYPE = "BaseExpandRecyclerAdapter".hashCode() + 1;
    protected static final int CHILD_TYPE = "BaseExpandRecyclerAdapter".hashCode() + 2;
    private final List<Pair<Parent, List<Child>>> mData = new ArrayList<>();

    private final List<Pair<Integer, Object>> mAdapterData = new ArrayList<>();

    private final List<Integer> mExpandParentPositionList = new ArrayList<>();

    public BaseExpandRecyclerAdapter() {
    }

    @SuppressLint("NotifyDataSetChanged")
    public void notifyData(List<Pair<Parent, List<Child>>> data) {
        mData.clear();
        mData.addAll(data);
        notifyAdapterDataSourcesChange();
        notifyDataSetChanged();
    }

    private void notifyAdapterDataSourcesChange() {
        mAdapterData.clear();
        if (mExpandParentPositionList.isEmpty()) {
            mAdapterData.addAll(mData.stream().map(it -> Pair.create(getItemViewTypeFromObject(it.first, PARENT_TYPE), (Object) it.first)).collect(Collectors.toList()));
            return;
        }
        for (int i = 0; i < mData.size(); i++) {
            mAdapterData.add(Pair.create(PARENT_TYPE, getParentData(i)));

            if (mExpandParentPositionList.contains(i)) {
                List<Child> childList = getChildList(i);
                mAdapterData.addAll(childList.stream().map(it -> Pair.create(getItemViewTypeFromObject(it, CHILD_TYPE), (Object) it)).collect(Collectors.toList()));
            }
        }
    }

    private int notifyAdapterDataSourcesInsert(int newExpandPosition) {
        int insertStart = getAdapterPosition(newExpandPosition) + 1;
        mAdapterData.addAll(insertStart, getChildList(newExpandPosition).stream().map(it -> Pair.create(getItemViewTypeFromObject(it, CHILD_TYPE), (Object) it)).collect(Collectors.toList()));
        return insertStart;
    }

    private int notifyAdapterDataSourcesRemove(int oldExpandPosition) {
        int removeStart = getAdapterPosition(oldExpandPosition) + 1;
        int count = getChildList(oldExpandPosition).size();

        Iterator<Pair<Integer, Object>> iterator = mAdapterData.iterator();

        int i = 0;
        while (iterator.hasNext()) {
            iterator.next();
            if (i >= removeStart && i < removeStart + count) {
                iterator.remove();
            }
            i++;
            if (i >= removeStart + count) {
                break;
            }
        }

        return removeStart;
    }


    protected int getAdapterPosition(int parentPosition) {
        if (mExpandParentPositionList.isEmpty()) {
            return parentPosition;
        }
        int count = mExpandParentPositionList.stream().filter(it -> parentPosition > it).mapToInt(it -> getChildList(it).size()).sum();

        return parentPosition + count;
    }


    /**
     * 展开
     */
    public void notifyExpand(int parentPosition) {
        boolean nextExpandStatus = !isExpand(parentPosition);
        notifyExpand(parentPosition, nextExpandStatus);
    }


    public void notifyExpand(int parentPosition, boolean expand) {
        if (!onSupportParentExpand(parentPosition, expand)) {
            return;
        }
        int index = mExpandParentPositionList.indexOf(parentPosition);
        int size = getChildList(parentPosition).size();
        if (index >= 0) {
            if (expand) {
                return;
            }
            mExpandParentPositionList.remove(index);
            int removeStart = notifyAdapterDataSourcesRemove(parentPosition);
            notifyItemRangeRemoved(removeStart, size);
            notifyItemRangeChanged(removeStart, mAdapterData.size() - removeStart);
        } else {
            if (!expand) {
                return;
            }
            mExpandParentPositionList.add(parentPosition);
            int insertStart = notifyAdapterDataSourcesInsert(parentPosition);
            notifyItemRangeInserted(insertStart, size);
            notifyItemRangeChanged(insertStart, mAdapterData.size() - insertStart);
        }
    }

    public boolean isExpand(int parentPosition) {
        return mExpandParentPositionList.contains(parentPosition);
    }

    public List<Pair<Parent, List<Child>>> getData() {
        return mData;
    }

    public List<Pair<Integer, Object>> getAdapterData() {
        return mAdapterData;
    }

    public Parent getParentData(int parentPosition) {
        return mData.get(parentPosition).first;
    }

    public Child getChildData(int parentPosition, int childPosition) {
        return mData.get(parentPosition).second.get(childPosition);
    }

    @Override
    public int getItemViewType(int position) {
        return mAdapterData.get(position).first;
    }

    @NonNull
    @Override

    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == PARENT_TYPE) {
            return onParentViewHolder(parent);
        }

        if (viewType == CHILD_TYPE) {
            return onChildViewHolder(parent);
        }

        return onCreateCommonViewHolder(parent, viewType);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Pair<Integer, Object> data = mAdapterData.get(position);
        int type = data.first;
        if (type == PARENT_TYPE) {
            holder.itemView.setOnClickListener(v -> {
                int parentPosition = getParentPosition(position);
                notifyExpand(parentPosition);

                onParentClick(holder, parentPosition);
            });

            onBindParentViewHolder(holder, (Parent) data.second);
        } else if (type == CHILD_TYPE) {
            holder.itemView.setOnClickListener(v -> {
                int parentPosition = getParentPosition(position);
                onChildClick(holder, parentPosition, position - getAdapterPosition(parentPosition) - 1);
            });
            onBindChildViewHolder(holder, (Child) data.second);
        } else {
            onBindCommonViewHolder(holder, position, data.second);
        }

    }

    private List<Child> getChildList(int parentPosition) {
        return mData.get(parentPosition).second;
    }

    protected int getParentPosition(int adapterPosition) {
        if (mExpandParentPositionList.isEmpty()) {
            return adapterPosition;
        }

        List<Integer> validExpandPosition = mExpandParentPositionList.stream().filter(it -> it < adapterPosition).sorted().collect(Collectors.toList());

        if (validExpandPosition.isEmpty()) {
            return adapterPosition;
        }

        int count = 0;
        for (int expandPosition : validExpandPosition) {
            //check last
            if (adapterPosition < expandPosition + count) {
                break;
            }
            count += getChildList(expandPosition).size();
            //check this
            if (adapterPosition <= expandPosition + count) {
                return expandPosition;
            }
        }

        // find 绝对位置
        return adapterPosition - count;
    }

    @Override
    public int getItemCount() {
        return mAdapterData.size();
    }


    protected abstract RecyclerView.ViewHolder onParentViewHolder(ViewGroup parent);

    protected abstract RecyclerView.ViewHolder onChildViewHolder(ViewGroup parent);

    /**
     * 存在其他类型的viewHolder时，需要重写的方法，
     *
     * @see #getItemViewType(int)
     */
    protected RecyclerView.ViewHolder onCreateCommonViewHolder(ViewGroup parent, int viewType) {
        throw new IllegalArgumentException("unSupport type " + viewType);
    }

    protected void onBindCommonViewHolder(RecyclerView.ViewHolder holder, int position, Object data) {
        throw new IllegalArgumentException("unSupport  CommonViewHolder " + holder);
    }

    /**
     * 根据object返回自定义Type
     *
     * @param object:数据
     * @param defaultType :默认类型{@link #CHILD_TYPE} {@link #PARENT_TYPE}
     */
    protected int getItemViewTypeFromObject(Object object, int defaultType) {
        return defaultType;
    }


    protected abstract void onBindChildViewHolder(RecyclerView.ViewHolder holder, Child data);

    protected abstract void onBindParentViewHolder(RecyclerView.ViewHolder holder, Parent data);

    /**
     * 是否支持parentPosition的下一个展开状态
     *
     * @param parentPosition   目标位置
     * @param nextExpandStatus 下一个展开状态
     */
    protected abstract boolean onSupportParentExpand(int parentPosition, boolean nextExpandStatus);

    protected void onParentClick(RecyclerView.ViewHolder holder, int parentPosition) {

    }

    protected void onChildClick(RecyclerView.ViewHolder holder, int parentPosition, int childPosition) {

    }
}
